#include <iostream>
#include <unordered_map>
#define MAXN 4005
using namespace std;
typedef long long  ll;
int N, T;
int A[MAXN], B[MAXN], C[MAXN], D[MAXN];
void solve();
int main() {
    ios::sync_with_stdio(false);
    cin >> T;
    while (T--) {
        cin >> N;
        for (int i = 0; i < N; i++) cin >> A[i] >> B[i] >> C[i] >> D[i];
        solve();
    }
}

void solve() {
    unordered_map<int, int> mapper;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            mapper[C[i] + D[j]]++;
    int res = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            int target = -A[i] - B[j];
            res += mapper[target];
        }
    }
    cout << res << endl;
}